package com.duobaoyu.dby.supply.biz.enums;

import com.duobaoyu.dby.common.mybatis.enums.IBaseEnum;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.supply.biz.exception.SupplyChainBizException;
import com.google.common.collect.Lists;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 订单补单状态
 *
 * @author chenms
 * @date 2023/10/17 3:18 PM
 */
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum OrderRepairStatusEnum implements IBaseEnum<Integer, String>{

    WAIT_REPAIR(1, "待补单"),
    REPAIRING(2, "补单中"),
    REPAIR_FAILED(3, "补单失败"),
    REPAIR_COMPLETE(4, "已补单"),
    CLOSED(5, "已关闭");

    private final Integer val;
    private final String desc;

    public static String getDescByStatus(Integer repairStatus) {
        return Arrays.stream(OrderRepairStatusEnum.values()).filter(orderRepairStatusEnum -> EnumUtils.eq(orderRepairStatusEnum,repairStatus)).map(OrderRepairStatusEnum::getDescribe).findFirst().orElse(null);
    }

    /**
     * 是否允许补单操作
     *
     * @param status 补单状态
     * @return true-允许
     */
    public static boolean isAllowRepair(Integer status) {
        return EnumUtils.eq(WAIT_REPAIR,status) || EnumUtils.eq(REPAIR_FAILED,status);
    }

    public static OrderRepairStatusEnum getByValue(Integer status) {
        return Arrays.stream(OrderRepairStatusEnum.values()).filter(statusEnum -> EnumUtils.eq(statusEnum,status)).findFirst().orElse(null);
    }


    /**
     * 获取前置状态
     */
    public static List<Integer> getPreStatusList(Integer newStatus) {
        OrderRepairStatusEnum statusEnum = getByValue(newStatus);
        if(Objects.isNull(statusEnum)) {
            return null;
        }
        List<Integer> preStatusList = null;
        switch (statusEnum) {
            case WAIT_REPAIR:
                preStatusList = null;
                break;
            case REPAIRING:
                preStatusList = Lists.newArrayList(WAIT_REPAIR.getValue(), REPAIR_FAILED.getValue());
                break;
            case REPAIR_FAILED:
            case REPAIR_COMPLETE:
                preStatusList = Lists.newArrayList(REPAIRING.getValue());
                break;
            case CLOSED:
                preStatusList = Lists.newArrayList(WAIT_REPAIR.getValue(), REPAIR_FAILED.getValue());
                break;
            default:
                SupplyChainBizException.fail("类型不存在");
        }
        return preStatusList;
    }

    @Override
    public Integer getValue() {
        return this.val;
    }


	@Override
	public String getDescribe(){
		return this.desc;
	}
}
